<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Keyword extends CI_Controller {

    /**
     * Crawl Weibo Content And Info By Keyword
     *
     */
    const SINA          = 'sina';
    const YUNYUN        = 'yunyun';
    const NETEASE       = 'netease';
    const SOHU          = 'sohu';
    const TENCENT       = 'tencent';
    const PLATFORM      = 'platform';
    const ADMIN         = 'soften';
    const ERROR_TAB     = 'error';
    const LOG_LEVEL_ERROR = 'error';
    const LOG_LEVEL_WARING = 'waring';
    const LOG_LEVEL_NOTICE = 'notice';
    const twitter='twitter';
    public $_log_dir    = 'keyword';
    public $_result_dir = 'result';
    public $_return_data;

    public function __construct(){
        parent::__construct();
        $this->load->helper('MY_auth_verification');
        $this->load->library('error_code_library');
        $this->load->library('crawl_abstract');
        //$this->load->database();
        $this->_return_data = $this->config->item('return_data');
    }

    public function index(){
		$this->get_keyword();
    	//$this->crawl();
	}

    public function get_keyword() {
        $auth_verification_result = authentication($this, $this->input->post());
        if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB,$auth_verification_result)) {
            echo json_encode($auth_verification_result);
            exit;
        } else {
            $validate_result = parameter_validate($this, $this->input->post());

            if(is_array($validate_result) && array_key_exists(self::ERROR_TAB,$validate_result)) {
                echo json_encode($auth_verification_result);
                exit;
            } else {

                $this->crawl();
            }
        }
    }

    private function crawl() {
        $this->load->library('crawl_keyword_library');
        
		$crawl_history = $this->crawl_keyword_library->find_in_cache();
        $this->_log_dir = $this->_log_dir.'/'.trim($this->input->post('platform'));
        $this->_result_dir = $this->_log_dir.'/'.$this->_result_dir;
        
        if(is_null($crawl_history)) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl BY '.json_encode($this->input->post()), FALSE, $this->_log_dir);
            $platform = trim($this->input->post(self::PLATFORM));
            $soften = trim($this->input->post("soften"));
            
            $html = trim($this->input->post("html"));

            if($platform == self::SINA) {
            	
            	//除非指定html抓取，否则使用默认抓取
            	if($html == "yes"){
            		$result = $this->sina_html_crawl();
            	}else{
            		$result = $this->sina_crawl();
            	}
                
            }
            else if($platform == self::twitter && $soften == "yes")
            {
            	$result=$this->twitter_crawl();

            }
            else if($platform == self::YUNYUN) {
                $result = $this->yunyun_crawl();
            } else if($platform == self::NETEASE) {
                
                //$result = $this->netease_crawl();
                
            	//网易微博已经停了
            	$result = array('error'=>'disable crawl netease !');
            } else if($platform == self::SOHU) {
                
                //$result = $this->sohu_crawl();
            	
            	//搜狐微博缺少接口
            	$result = array('error'=>'disable crawl sohu !');
            	
            } else if($platform == self::TENCENT) {
            	//$result = array('error'=>'disable crawl tencent !');
                $result = $this->tencent_crawl();
            } else {
                $result = json_encode($this->error_code_library->getError('system', 'params', 'no_platform', $platform));
                log_message(self::LOG_LEVEL_ERROR, 'NO Result: '.$result, FALSE, $this->_log_dir);
                echo $result;exit;
            }

            if(is_null($result) || array_key_exists(self::ERROR_TAB, $result) == FALSE) {
                if(array_key_exists('data', $result)) {
                    $result_string = json_encode($this->_return_data);
                } else {
                    $this->_return_data['data'] = $result;
                    $result_string = json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data));
                    $this->crawl_keyword_library->save_crawl_result($result_string);
                }

                log_message(self::LOG_LEVEL_NOTICE, 'Return Result Crawl String: '.$result_string, FALSE, $this->_result_dir);

                echo $result_string;
            } else {
            	//如果是soften自己使用的..则直接返回原始错误信息
                if($this->input->post(self::ADMIN) === FALSE  && $this->input->get(self::ADMIN) === FALSE) {
                    $this->_return_data['error'] = $this->error_code_library->getError('system', 'common', 'failed');
                } else {
                    $this->_return_data['error'] = $result;
                }
                log_message(self::LOG_LEVEL_ERROR, 'Crawl Error: '.json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data)), FALSE, $this->_log_dir);
                echo json_encode($this->_return_data);
            }
        } else {
            log_message(self::LOG_LEVEL_NOTICE, 'Return Crawl String From Cache: '.$crawl_history, FALSE, $this->_result_dir);
            echo $crawl_history;
        }
    }

    private function sina_crawl() {
        $this->load->library('Crawl_sina_keyword_from_phone_library');
        $html_crawl_result = $this->crawl_sina_keyword_from_phone_library->crawl(TRUE);
        if(array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE && $html_crawl_result != FALSE) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Sina HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            return $html_crawl_result;
        } else if(array_key_exists(self::ERROR_TAB, $html_crawl_result) && FALSE == $this->crawl_abstract->not_illegal_error($html_crawl_result)) {
            return $html_crawl_result;
        }else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From Sina HTML Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_log_dir);
            
            //2014-12-18 Stop Crawl From Sina html

			/*$this->load->library('Crawl_sina_keyword_from_html_library');
            $wap_crawl_result = $this->crawl_sina_keyword_from_html_library->crawl(TRUE);
            if($wap_crawl_result == FALSE || (is_array($wap_crawl_result) && array_key_exists(self::ERROR_TAB, $wap_crawl_result))) {
                log_message(self::LOG_LEVEL_ERROR, 'Crawl From Sina WAP Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($wap_crawl_result)), FALSE, $this->_log_dir);
            } else {
                log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Sina WAP Finish And Result Is: '.json_encode($wap_crawl_result), FALSE, $this->_result_dir);
            }
            return $wap_crawl_result;*/

            return $html_crawl_result;
        }
    }
    
    private function sina_html_crawl() {
    	$this->load->library('Crawl_sina_keyword_from_html_library');
    	$html_crawl_result = $this->crawl_sina_keyword_from_html_library->crawl(true);
    	if(array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE && $html_crawl_result != FALSE) {
    		log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Sina HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
    		return $html_crawl_result;
    	} else if(array_key_exists(self::ERROR_TAB, $html_crawl_result) && FALSE == $this->crawl_abstract->not_illegal_error($html_crawl_result)) {
    		return $html_crawl_result;
    	}else {
    		log_message(self::LOG_LEVEL_ERROR, 'Crawl From Sina HTML Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_log_dir);
  
    		return $html_crawl_result;
    	}
    }

    private function yunyun_crawl() {
        $this->load->library('Crawl_yunyun_keyword_from_html_library');
        $html_crawl_result = $this->crawl_yunyun_keyword_from_html_library->crawl(TRUE);

        if($html_crawl_result != FALSE && is_array($html_crawl_result) && array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From YumYum Html Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            return $html_crawl_result;
        } else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From YumYum Html Failed: '.json_encode($html_crawl_result), FALSE, $this->_log_dir);
            return $html_crawl_result;
        }
    }

  	private function tencent_crawl() {
        $this->load->library('Crawl_tencent_keyword_from_html_library');
        $client_crawl_result = $this->crawl_tencent_keyword_from_html_library->crawl(true);
        if($client_crawl_result == FALSE || (is_array($client_crawl_result) && (array_key_exists(self::ERROR_TAB, $client_crawl_result)) || (array_key_exists('data', $client_crawl_result) && is_null($client_crawl_result['data'])))) {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From Tencent CLIENT Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($client_crawl_result)), FALSE, $this->_log_dir);
            return $client_crawl_result;

            /*
		    $this->load->library('Crawl_keyword_from_api_library');
		    $api_crawl_result = $this->crawl_keyword_from_api_library->crawl(TRUE);
            if(array_key_exists(self::ERROR_TAB, $api_crawl_result) === FALSE && $api_crawl_result != FALSE) {
                log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Tencent API Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($api_crawl_result)), FALSE, $this->_result_dir);
            } else {
                log_message(self::LOG_LEVEL_ERROR, 'Crawl From Tencent API Failed: '.json_encode($api_crawl_result), FALSE, $this->_log_dir);
            }

            return $api_crawl_result;*/

        } else {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Tencent CLIENT Finish And Result Is: '.json_encode($client_crawl_result), FALSE, $this->_result_dir);
            return $client_crawl_result;
        }
    }

    private function tencent_crawl_bak() {
        $this->load->library('Crawl_keyword_from_api_library');
        $api_crawl_result = $this->crawl_keyword_from_api_library->crawl(TRUE);
        if($api_crawl_result == FALSE || (is_array($api_crawl_result) && (array_key_exists(self::ERROR_TAB, $api_crawl_result)) || (array_key_exists('data', $api_crawl_result) && is_null($api_crawl_result['data'])))) {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From Tencent API Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($api_crawl_result)), FALSE, $this->_log_dir);
            return $api_crawl_result;

			 //2014-01-14 Stop Crawl From tencent html
			/*
			$this->load->library('Crawl_tencent_keyword_from_html_library');
            $html_crawl_result = $this->crawl_tencent_keyword_from_html_library->crawl(TRUE);
            if(array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE && $html_crawl_result != FALSE) {
                log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Tencent HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            } else {
                log_message(self::LOG_LEVEL_ERROR, 'Crawl From Tencent HTML Failed: '.json_encode($html_crawl_result), FALSE, $this->_log_dir);
            }
            return $html_crawl_result;
			*/
        } else {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Tencent API Finish And Result Is: '.json_encode($api_crawl_result), FALSE, $this->_result_dir);
            return $api_crawl_result;
        }
    }

    private function netease_crawl() {
        $this->load->library('Crawl_netease_keyword_from_html_library');
        $html_crawl_result = $this->crawl_netease_keyword_from_html_library->crawl(TRUE);
        if($html_crawl_result != FALSE && is_array($html_crawl_result) && array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From NETEASE HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            return $html_crawl_result;
        } else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From NETEASE HTML Failed: '.json_encode($html_crawl_result), FALSE, $this->_log_dir);
            return $html_crawl_result;
        }
    }

    private function sohu_crawl() {
        $this->load->library('Crawl_keyword_from_api_library');
        $api_crawl_result = $this->crawl_keyword_from_api_library->crawl(TRUE);
        if(is_null($api_crawl_result) || (array_key_exists(self::ERROR_TAB, $api_crawl_result) === FALSE && $api_crawl_result !== FALSE)) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From SOHU API Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($api_crawl_result)), FALSE, $this->_result_dir);
            return $api_crawl_result;
        } else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From SOHU API Failed: '.json_encode($api_crawl_result), FALSE, $this->_log_dir);
            return $api_crawl_result;
        }
    }
    
    
    private  function  twitter_crawl()
    {

    	$this->load->library('Crawl_twitter_keyword_from_html_library');
     	
    	$twitter_result = $this->crawl_twitter_keyword_from_html_library->crawl();
    	
    	return  $twitter_result;
    
    }
    

    public function test() {
        $this->load->helper('url');
        $this->load->library('table');
        header('Content-Type:text/html;charset=utf-8');
        $post_data = $this->input->post();
        $post_data['password']="112358s" ;
        if($post_data['password'] != "112358s"){
        	echo "hehe";
        	exit;
        } 
        $post_data['platform'] = $this->input->post('platform') !== FALSE ? trim($this->input->post('platform')):"sina";
        $post_data['word'] = $this->input->post('word') !== FALSE ? trim($this->input->post('word')):"郭美美";
        $post_data['method'] = "POST";
        $post_data['token'] = "9f7ffa5b38e13b1843eeffcc3ba06565";
        $post_data['page']  = $this->input->post('page') !== FALSE ? trim($this->input->post('page')) : 1;
        $post_data['soften'] = "yes";

        // var_dump($post_data);
        
        //如果是腾讯平台的数据.将传入的timescope参数..作为starttime.只抓取最近指定时间段内的数据
        if($post_data['platform'] == 'tencent') {
        	if(strpos($post_data['timescope'],'-')) $post_data['starttime'] = strtotime($post_data['timescope']);
        	else $post_data['starttime'] = $post_data['timescope'];
        }


        
        //测试接口..添加调试信息
        if($this->input->post('soften_admin_debug')) {
        	$soften_admin_debug = true;
        	$post_data['soften_admin_debug'] = $soften_admin_debug;
        }

        //$post_data = http_build_query($post_data);
        if($this->config->item('test_use_proxy') == TRUE) {
            $url= $this->config->item('test_proxy_add')."keyword/index";
        } else {
            $url= site_url("keyword/index");
        }
        
  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        if ($result === FALSE) {
            echo "CURL Error In Test Connect API: " . curl_error($ch). " TO ".$url;
        }
        curl_close($ch);

        //测试接口..添加调试信息
        if($soften_admin_debug){
        	var_dump($result);
        	return ;
        }

        $wbfield = array(
            '微博ID', '微博信息内容', '微博创建时间', '微博来源', '转', '评', '好', '转载的微博ID', '转载的微博用户ID', '转载的微博用户名', '详情URL',
            '缩略图片地址', '中等尺寸图片地址', '原始图片地址', '增加时间', '渠道ID抓取渠道', '用户ID', '用户空间URL', '昵称', '描述', '头像', '粉丝数', '关注数', '是否加V'
        );

        if(is_null($result) ) {
            echo '无数据';
        } else {
            $result_data = json_decode($result, TRUE);
            if(is_null($result_data)) {
            	var_dump($result);
                echo '返回结果解析失败';die;
                
            }

            if(array_key_exists(self::ERROR_TAB, $result_data) && !empty($result_data[self::ERROR_TAB])) {
                echo '抓取错误<br/>';
                var_dump($result_data);
            } elseif(array_key_exists('data', $result_data) && is_null($result_data['data'])) {
                echo '无数据<br/>';
                var_dump($result_data);
            }else {
                echo '<head><meta charset="utf-8"><title>'.$post_data['platform'].'---'.$post_data['word'].'---'.$post_data['page'].'</title><style>*{font-size:12px}</style></head>';
                $tmpl = array ( 'table_open'  => '<table border="1" cellpadding="0" cellspacing="0" class="mytable">' );
                echo count($result_data['data']).'条记录';
                $this->table->set_template($tmpl);
                $this->table->set_heading($wbfield);
                foreach($result_data['data'] as $row) {
                    $data = array();
                    $data[] = $row['_id'];
                    $data[] = $row['text'];
                    $data[] = $row['ctime'];
                    $data[] = $row['source'];
                    $data[] = $row['reposts_count'];
                    $data[] = $row['comments_count'];
                    $data[] = $row['attitudes_count'];
                    $data[] = $row['retweeted_id'];
                    $data[] = $row['retweeted_uid'];
                    $data[] = $row['retweeted_name'];
                    $data[] = empty($row['url'])? '': "<a href='{$row['url']}' target='_blank'>{$row['url']}</a>";
                    $data[] = $row['thumbnail_pic'];
                    $data[] = $row['bmiddle_pic'];
                    $data[] = $row['original_pic'];
                    $data[] = $row['addtime'];
                    $data[] = $row['source_myid'];
                    $data[] = $row['user']['id'];
                    $data[] = empty($row['user']['profile_url'])? '': "<a href='{$row['user']['profile_url']}' target='_blank'>{$row['user']['profile_url']}</a>";
                    $data[] = $row['user']['name'];
                    $data[] = $row['user']['description'];
                    $data[] = $row['user']['header_url'];
                    $data[] = $row['user']['followers_count'];
                    $data[] = $row['user']['friends_count'];
                    $data[] = $row['user']['verified'];
                    $this->table->add_row($data);
                }
                echo $this->table->generate();
            }
        }

    }
}
